const formatTime = date => {
  date = new Date(date);
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatdate = date => {
  date = new Date(date);
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('.');
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
/**
 * 手机号15689003415--》156 8900 3415
 * 手机号15689003415--》156-8900-3415
 */
const formatPhoneNum = (val, rules) => {
  val = val.toString()
  let tagString = '';
  if (rules) {
    tagString = val.replace(/(\d{3})(\d{0,4})(\d{0,4})/, '$1' + rules + '$2' + rules + '$3')
  } else {
    tagString = val.replace(/(\d{3})(\d{0,4})(\d{0,4})/, '$1 $2 $3')
  }
  return tagString
}
/**
 * 根据key查找数据中对应的值
 */
const SearchData = (key, data) => {
  var value = '';
  var i = 0;
  while (i < data.length) {

    if (key == data[i].key) {
      value = data[i].title;
      break;
    } else {
      i++;
      continue;
    }
  }
  return value;
}

/**
 * 根据key查找数据中对应角标
 */
const SearchDataIndex = (key, data) => {
  var value = '';
  var i = 0;
  while (i < data.length) {

    if (key == data[i].key) {
      value = i;
      break;
    } else {
      i++;
      continue;
    }
  }
  return value;
}
//转换资金千分位
const transNum = function (str) {
  var num = Number(str);
  if (isNaN(num)) {
    return str;
  } else {
    num = num.toFixed(3).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
    return num;
  }
}
//数字补零
const returnFloat = function (value) {
  var value = Math.round(parseFloat(value) * 100) / 100
  var xsd = value.toString().split('.')
  if (xsd.length == 1) {
    value = value.toString() + '.00'
    return value
  }
  if (xsd.length > 1) {
    if (xsd[1].length < 2) {
      value = value.toString() + '0'
    }
    return value
  }
}

const transNums = function (str) {
  var num = Number(str);
  if (isNaN(num)) {
    return str;
  } else {
    num = num.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
    return num;
  }
}
//校验邮箱格式
const checkEmail = (email) => {
  return RegExp(/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/).test(
    email)
}
//校验手机格式
const checkMobile = (mobile) => {
  return RegExp(/^1[34578]\d{9}$/).test(mobile)
}
// 输入数值为正准数
const v_integer = (title) => {
  return (rule, value, callback) => {
    const integer = /^[1-9]*[1-9][0-9]*$/
    if (!value) callback(new Error(title))
    else if (!integer.test(value))
      callback(new Error(`输入的数字必须为正整数且不能以0开头`))
    else callback()
  }
}
const bigImageUrl = (cateId) => {
  switch (cateId) {
    case "线材":
      return "http://www.sxjbjt.com:9081/img/xiancai.jpg"
      break;
    case "螺纹钢":
      return "http://www.sxjbjt.com:9081/img/luowengang.jpg"
      break;
    case "盘螺":
      return "http://www.sxjbjt.com:9081/img/panluo.jpg"
      break;
    case "锚杆钢":
      return "http://www.sxjbjt.com:9081/img/maogangang.png"
      break;
    case "生铁":
      return "http://www.sxjbjt.com:9081/img/shengtie.jpg"
      break;
    case "石油管坯":
      return "http://www.sxjbjt.com:9081/img/shiyouguanpi.jpg"
      break;
    case "钢坯":
      return "http://www.sxjbjt.com:9081/img/gangpi.jpg"
      break;
    case "号钢":
      return "http://www.sxjbjt.com:9081/img/jbhaogang.png"
      break;
    case "30MnSi":
      return "http://www.sxjbjt.com:9081/img/xiancai.jpg"
      break;
    case "钢绞线":
      return "http://www.sxjbjt.com:9081/img/jbgangjiaoxian.png"
      break;
    case "炼钢铁":
      return "http://www.sxjbjt.com:9081/img/jbshengtie.png"
      break;
    case "铸造铁":
      return "http://www.sxjbjt.com:9081/img/jbshengtie.png"
      break;
    case "高纯生铁":
      return "http://www.sxjbjt.com:9081/img/jbshengtie.png"
      break;
    case "球墨铸铁":
      return "http://www.sxjbjt.com:9081/img/jbqiumo.png"
      break;
    case "优质球墨铸铁":
      return "http://www.sxjbjt.com:9081/img/jbqiumo.png"
      break;
    case "水渣":
      return "http://www.sxjbjt.com:9081/img/jbshuizha.png"
      break;
    default:
      return "http://www.mescroll.com/img/mescroll-empty.png?v=1"
      break;
  }

}
const imageUrl = (cateId) => {
  switch (cateId) {
    case "线材":
      return "http://www.sxjbjt.com:9081/img/xiancai.jpg"
      break;
    case "螺纹钢":
      return "http://www.sxjbjt.com:9081/img/luowengang.jpg"
      break;
    case "盘螺":
      return "http://www.sxjbjt.com:9081/img/panluo.jpg"
      break;
    case "锚杆钢":
      return "http://www.sxjbjt.com:9081/img/maogangang.png"
      break;
    case "生铁":
      return "http://www.sxjbjt.com:9081/img/shengtie.jpg"
      break;
    case "石油管坯":
      return "http://www.sxjbjt.com:9081/img/shiyouguanpi.jpg"
      break;
    case "钢坯":
      return "http://www.sxjbjt.com:9081/img/gangpi.jpg"
      break;
    case "号钢":
      return "http://www.sxjbjt.com:9081/img/jbhaogang.png"
      break;
    case "30MnSi":
      return "http://www.sxjbjt.com:9081/img/xiancai.jpg"
      break;
    case "钢绞线":
      return "http://www.sxjbjt.com:9081/img/jbgangjiaoxian.png"
      break;
    case "炼钢铁":
      return "http://www.sxjbjt.com:9081/img/jbshengtie.png"
      break;
    case "铸造铁":
      return "http://www.sxjbjt.com:9081/img/jbshengtie.png"
      break;
    case "高纯生铁":
      return "http://www.sxjbjt.com:9081/img/jbshengtie.png"
      break;
    case "球墨铸铁":
      return "http://www.sxjbjt.com:9081/img/jbqiumo.png"
      break;
    case "优质球墨铸铁":
      return "http://www.sxjbjt.com:9081/img/jbqiumo.png"
      break;
    case "水渣":
      return "http://www.sxjbjt.com:9081/img/jbshuizha.png"
      break;
    default:
      return "http://www.mescroll.com/img/mescroll-empty.png?v=1"
      break;
  }

}
/**
 * 处理后台返回数据包含 null 给默认处理 --- 的函数
 * @param {*} obj 返回的 result 数据源
 */

const d_resNull = (obj) => {
  for (let item in obj) {
    let resultChild = obj[item]
    if (typeof resultChild === 'object') {
      d_resNull(resultChild)
    }
    for (let key in resultChild) {
      if ((resultChild[key] === null || resultChild[key] == 'null') && key !== 'children') {
        resultChild[key] = '-'
      }
    }
  }
  return obj
}
module.exports = {
  formatTime,
  formatdate,
  SearchData,
  SearchDataIndex,
  checkEmail,
  checkMobile,
  imageUrl,
  d_resNull,
  bigImageUrl,
  transNum,
  returnFloat,
  transNums,
  formatPhoneNum,
  v_integer
}
